const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const os = require('os');
const app = express();
const port = 3000;

app.use(bodyParser.json());
app.use(cors());

function getLocalIpAddress() {
    const interfaces = os.networkInterfaces();
    for (const name of Object.keys(interfaces)) {
        for (const iface of interfaces[name]) {
            if (iface.family === 'IPv4' && !iface.internal) {
                return iface.address;
            }
        }
    }
    return 'localhost';
}

app.get('/server-ip', (req, res) => {
    const ip = getLocalIpAddress();
    res.json({ ip });
});

app.post('/configure-network', (req, res) => {
    const { ssid, password } = req.body;
    console.log('Received network configuration:');
    console.log('SSID:', ssid);
    console.log('Password:', password);
    res.json({ message: 'Network configuration received successfully' });
});

app.listen(port, () => {
    console.log(`Server is running at http://localhost:${port}`);
});
